package kitwee.com.kanpad.bean;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.blankj.utilcode.util.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kitwee.com.kanpad.R;

/**
 * Created by sven on 2016/11/9.
 */

public class MessageListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {



    private Context context;// 上下文.
    private List<ListDetailBean.KanbanMessageVOListBean> listBean = new ArrayList<>();// 数据集合
    private LayoutInflater mLayoutInflater;

    public MessageListAdapter(Context context, List<ListDetailBean.KanbanMessageVOListBean> mDatass) {
        mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.listBean = mDatass;
    }

    //定义枚举
    public static enum ITEM_TYPE {
        ITEM_TYPE_IMAGE, ITEM_TYPE_TEXT
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal()) {
//            return new MyHolder(mLayoutInflater.inflate(R.layout.recycle_item_layout, parent, false));
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bomvo_recycle_item_layout, parent, false);
            view.setBackgroundResource(R.color.white);
            MyHolder holder = new MyHolder(view);

            return holder;
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bomvo_recycle_item_layout, parent, false);
            view.setBackgroundResource(R.color.list_title_back_gray);
            MyHolder holder = new MyHolder(view);
            return holder;
        }
    }

    //绑定数据
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyHolder && listBean.size() > 0) {
            long effective_time = listBean.get(position).getEffective_time();
            String s = TimeUtils.millis2String(effective_time);
            ((MyHolder) holder).tvBomvoName.setText(s);
            ((MyHolder) holder).tvBomvoSum.setText(listBean.get(position).getSummary() + "");
        }

        /**
         * item 点击事件
         */
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }


    @Override
    public int getItemCount() {

        return listBean == null ? 0 : listBean.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_bomvo_name)
        TextView tvBomvoName;
        @BindView(R.id.tv_bomvo_sum)
        TextView tvBomvoSum;

        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemViewType(int position) {

        return position % 2 == 0 ? ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal() : ITEM_TYPE.ITEM_TYPE_TEXT.ordinal();
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

}
